package rmi;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class ServidorRMI {

	public void iniciar(){
		try {
			LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
			InterfazRemota ir = new ObjetoRemoto();
			Naming.rebind("//localhost/ObjetoRemoto", ir);
			verVinculos();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	
	public void verVinculos(){
		try {
			String[] vinculos = Naming.list("");
			System.out.println("Vinculos disponibles en el servidor: ");
			System.out.println(InetAddress.getLocalHost().getHostAddress());
			
			for(int i = 0; i < vinculos.length; i++){
				System.out.println(vinculos[i]);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	public void cerrar(){
		try {
			Naming.unbind("//localhost/ObjetoRemoto");
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}
}
